﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Logic.Common;
using Job.Data.Interfaces;
using Job.Entity;
using System.Data;
using System.Data.SqlClient;
using Job.Logic.Common;
using System.Collections;
using Job.Web.Framework;
namespace Job.Logic.BaseContent
{
    public class BaseContentLogic
    {
        private readonly ICommon commonDao = DataAccess.commonDao;
        private readonly IJobBaseContentDao baseContentDao = DataAccess.jobBaseContentDao;

        public void Insert(JobBaseContent baseContent)
        {
            baseContentDao.Insert(baseContent);
        }

        public void Update(JobBaseContent baseContent)
        {
            baseContentDao.Update(baseContent);
        }

        public JobBaseContent GetContent(int contentId)
        {
            return baseContentDao.Find(contentId);
        }

        public JobBaseContent GetContentByContentId(int contentId)
        {
            return baseContentDao.Find(contentId);
        }
        public JobBaseContent QuickGetContentByContentId(int contentId)
        {
            return baseContentDao.QuickFind(contentId);
        }

        public JobBaseContent GetContentBySubjectId(int subjectId)
        {
            IList<JobBaseContent> list = baseContentDao.FindBySubjectId(subjectId);
            if (list != null && list.Count >0)
            {
                return list[0];
            }
            else
            {
                return null;
            }

        }
        public JobBaseContent QuickGetContentBySubjectId(int subjectId)
        {
            IList<JobBaseContent> list = baseContentDao.QuickFindBySubjectId(subjectId);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        public void Delete(JobBaseContent content)
        {
            baseContentDao.Delete(content);
        }
        

        public DataSet GetBaseContentList(int pageSize, int pageIndex, int countType, string sortField, SortDirection sortDirection, ArrayList paramList)
        {
            string sql = @"select content_id,title,description,status_id,order_no,
                            subject_id,count,last_update_time,last_update_user
                            from job_base_content where status_id = 1 ";

            foreach (SqlParameter param in paramList)
            {
                switch (param.ParameterName)
                {
                    case "subject_id":
                        sql += " and subject_id = " + param.Value + "";
                        break;
                }
            }

            CommonLogic logic=new CommonLogic();
            DataSet ds = logic.GetPageDataBySqlTwoFieldOrder(sql, pageSize, pageIndex, countType, sortField, sortDirection, paramList);
            return ds;
        }


        //public List<JobBaseContent> GetTopRowListBySubjectId(int subjectId, int number)
        //{
        //    List<JobBaseContent> results = new List<JobBaseContent>();
        //    try
        //    {
        //        //String sql = "Select top " + Convert.ToString(number) + " * From ( "
        //        //    + "Select content_id,title,description,subject_id,count,"
        //        //    + "last_update_time,order_no From job_base_content "
        //        //    + "Where subject_id = " + Convert.ToString(subjectId)
        //        //    + " And status_id = 1 ) a "
        //        //    + "Order By order_no desc,content_Id desc";

        //        string sql = "Select content_id,title,description,subject_id,count,"
        //            + "last_update_time,order_no From job_base_content "
        //            + "Where subject_id = " + Convert.ToString(subjectId)
        //            + " And status_id = 1 ";

        //        CommonLogic logic = new CommonLogic();
        //        DataSet ds = logic.GetPageDataBySql(sql, number, 1, 0, "order_no", SortDirection.Descending, null);
        //        DataTable dt = ds.Tables[0];

        //        foreach (DataRow row in dt.Rows)
        //        {
        //            JobBaseContent content = new JobBaseContent();

        //            content.ContentId = (int)row["content_id"];
        //            content.Title = (string)row["title"];
        //            content.Description = (string)row["description"];
        //            content.SubjectId = (int)row["subject_id"];
        //            content.Count = (int)row["count"];
        //            content.LastUpdateTime = (DateTime)row["last_update_time"];

        //            results.Add(content);
        //        }

        //    }
        //    catch(Exception ex)
        //    {
        //        Logger.WriteLog(ex.Message);
        //    }
        //    return results;
        //}


    }
}
